using System;
using System.Collections;
using System.Collections.Generic;
using QFramework;
using UnityEngine;
using WuLibrary;


namespace  TowerDefence_QF
{
    public class EnemyController : BaseController
    {
        private Enemydata mEnemyData=new Enemydata();
        private List<Vector3> mMapPathList;
        private int mCurrentMapIndex;
        private LevelSystem mLevelSystem;

       
        public void InitEnemy(List<Vector3> mapPathList,Enemydata enemydata,LevelSystem levelSystem)
        {
            if (mLevelSystem == null)
            {
                mLevelSystem=levelSystem;
            }
            
            mMapPathList = mapPathList;
            mCurrentMapIndex = 0;
            this.LocalPosition(mMapPathList[mCurrentMapIndex]);
            
            mEnemyData.ID=enemydata.ID;
            mEnemyData.HP=enemydata.HP;
            mEnemyData.Name=enemydata.Name;
            mEnemyData.MoveSpeed=enemydata.MoveSpeed;

            
            
            
        }
       

        

        private void Update()
        {
            Move();
        }

        public bool AreEnemiesAlive()
        {
           return mEnemyData.HP > 0;
        }

        private void Move()
        {
            if (mCurrentMapIndex < mMapPathList.Count-1)
            {
               
                if (mMapPathList.Count > 0)
                {
                    var index= mCurrentMapIndex + 1;
                    Vector3 pos=mMapPathList[index];

                    float rang;
                    //Z轴行走
                    if (transform.localPosition.x == pos.x)
                    {
                        rang=Mathf.Abs(transform.localPosition.z-pos.z);
                        if (transform.localPosition.z > pos.z)//下
                        {
                            transform.localEulerAngles = new Vector3(0, 180, 0);
                        }
                        else//上
                        {
                            transform.localEulerAngles = new Vector3(0, 0, 0);
                        }
                    
                    }
                    else//X轴行走
                    {
                        pos.z=transform.localPosition.z; 
                        rang=Mathf.Abs(transform.localPosition.x-pos.x);
                        if (transform.localPosition.x > pos.x)//左边
                        {
                            transform.localEulerAngles = new Vector3(0, 270, 0);
                        }
                        else//右边
                        {
                            transform.localEulerAngles = new Vector3(0, 90, 0);
                        }
                    
                    }
                    float totalTime = rang / mEnemyData.MoveSpeed;
                    if (Time.deltaTime >= totalTime)
                    {
                        transform.localPosition = pos;
                        mCurrentMapIndex += 1;

                    }
                    else
                        transform.localPosition = Vector3.Lerp(transform.localPosition,pos, Time.deltaTime / totalTime);
                
                }


            }

            else
            {
                this.SendCommand(new UdpateHpCmd());
                mLevelSystem.RecyleEnemy(this);
                ColorDebugTools.Log("到了",LogColor.Red);
            }
         
        }
        
        
    }
}

